package com.train.member.facade;

import cn.hutool.core.bean.BeanUtil;
import com.train.common.group.AddGroup;
import com.train.common.group.EditGroup;
import com.train.common.group.QueryGroup;
import com.train.common.resp.CommonResp;
import com.train.common.resp.PageResp;
import com.train.member.dto.PassengerAddOrEditDTO;
import com.train.member.dto.PassengerPageDTO;
import com.train.member.req.PassengerAddOrEditReq;
import com.train.member.req.PassengerPageReq;
import com.train.member.resp.passenger.PassengerPageResp;
import com.train.member.resp.passenger.PassengerListResp;
import com.train.member.service.PassengerService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("passenger")
public class PassengerFacade {

    private final PassengerService passengerService;

    @PostMapping("page")
    public CommonResp pageList(@RequestBody @Validated(QueryGroup.class) PassengerPageReq req) {
        PassengerPageDTO passengerPageDTO = BeanUtil.copyProperties(req, PassengerPageDTO.class);
        PageResp<PassengerPageResp> pageResp = passengerService.pageList(passengerPageDTO);
        return CommonResp.ok(pageResp);
    }

    @PostMapping("add")
    public CommonResp add(@RequestBody @Validated(AddGroup.class) PassengerAddOrEditReq req) {
        PassengerAddOrEditDTO passengerAddOrEditDTO = BeanUtil.copyProperties(req, PassengerAddOrEditDTO.class);
        passengerService.addOrEditPassenger(passengerAddOrEditDTO);
        return CommonResp.ok();
    }

    @PostMapping("edit")
    public CommonResp edit(@RequestBody @Validated(EditGroup.class) PassengerAddOrEditReq req) {
        PassengerAddOrEditDTO passengerAddOrEditDTO = BeanUtil.copyProperties(req, PassengerAddOrEditDTO.class);
        passengerService.addOrEditPassenger(passengerAddOrEditDTO);
        return CommonResp.ok();
    }

    @DeleteMapping("remove/{id}")
    public CommonResp delete(@PathVariable("id") Long id) {
        passengerService.remove(id);
        return CommonResp.ok();
    }

    @GetMapping("/list")
    public CommonResp passengerList() {
        List<PassengerListResp> resp = passengerService.queryPassengerList();
        return CommonResp.ok(resp);
    }
}
